package com.example.javatool.recallfile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileAssociation {
    public static void main(String[] args) {
        String mailFileBelongPath = "D:\\email\\mail_file_belong.txt";
        String mailFileMd5Path = "D:\\email\\mail_file_md5.txt";
        String serverFilePath = "D:\\email\\server\\server_file.txt";
        String outputFilePath = "D:\\email\\file_association.txt";

        try {
            // 读取 mail_file_belong.txt 文件数据
            Map<String, String> mailFileBelongMap = readMailFileBelong(mailFileBelongPath);
            // 读取 server_file.txt 文件数据
            Map<String, String> serverFileMap = readServerFile(serverFilePath);
            // 关联数据并生成映射关系
            Map<String, String> resultMap = associateData(mailFileBelongMap, serverFileMap, mailFileMd5Path);
            // 将映射关系保存到文件
            saveResultToFile(resultMap, outputFilePath);
            System.out.println("数据关联完成，结果已保存到 " + outputFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取 mail_file_belong.txt 文件数据
    private static Map<String, String> readMailFileBelong(String filePath) throws IOException {
        Map<String, String> mailFileBelongMap = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(" ");
                if (parts.length >= 2) {
                    mailFileBelongMap.put(parts[0], parts[1]);
                }
            }
        }
        return mailFileBelongMap;
    }

    // 读取 server_file.txt 文件数据
    private static Map<String, String> readServerFile(String filePath) throws IOException {
        Map<String, String> serverFileMap = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length >= 3) {
                    int lastIndex = parts[0].lastIndexOf("/");
                    String fileName = lastIndex != -1 ? parts[0].substring(lastIndex + 1) : parts[0];
                    serverFileMap.put(parts[2], fileName);
                }
            }
        }
        return serverFileMap;
    }

    // 关联数据并生成映射关系
    private static Map<String, String> associateData(Map<String, String> mailFileBelongMap, Map<String, String> serverFileMap, String mailFileMd5Path) throws IOException {
        Map<String, String> resultMap = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(mailFileMd5Path))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(" ");
                if (parts.length >= 2) {

                            String fileMd5 = parts[1];
                            String employee = parts[0].split("-")[0];
                            if (serverFileMap.containsKey(fileMd5)) {
                                String fileName = serverFileMap.get(fileMd5);
                                // 保存文件后缀名
                                int dotIndex = fileName.lastIndexOf('.');
                                String fileExtension = dotIndex != -1 ? fileName.substring(dotIndex) : "";
                                // 使用 "_" 拆分文件名
                                String[] fileNameParts = fileName.split("_");
                                // 若拆分结果至少有两个元素，取第二个值，否则使用原文件名
                                fileName = fileNameParts.length >= 2 ? fileNameParts[1] : fileName;
                                // 拼接文件名和后缀名
//                                fileName = fileName + fileExtension;
                                resultMap.put(parts[0], employee + "_" + fileName);
                            }


                }
            }
        }
        return resultMap;
    }

    // 将映射关系保存到文件
    private static void saveResultToFile(Map<String, String> resultMap, String outputFilePath) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
            for (Map.Entry<String, String> entry : resultMap.entrySet()) {
                writer.write(entry.getKey() + " " + entry.getValue());
                writer.newLine();
            }
        }
    }
}
